package com.enjia.common.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;

/**
 *  权限申请工具类。
 * Created by 张海洋 on 2017/6/30.
 */

public class PermissionsUtils {
    private static PermissionsUtils instance;
    private PermissionsUtils(){};//私有化构造方法
    public static final int requestPermissionCode = 100;

    /**
     * 公共方法，获取帮助类的单例对象
     */
    public static PermissionsUtils getInstance(){
        if(null == instance){
            instance = new PermissionsUtils();
        }
        return instance;
    }
    /**
     * 权限申请
     *
     * @param activity
     * @param permissions
     * new String[]  字符串数组，每一个元素即要申请的权限
     */
    public boolean checkRequestPermission(Activity activity, String... permissions) {
        boolean hasPermission = true;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            //如果sdk版本号小于23 ，则不用动态申请权限，直接返回
            return true;
        }
        //循环判断是否存在需要申请的权限，如果存在，将标志置为true
        for (String p : permissions) {
            if (!hasPermission(p, activity)) {
                hasPermission = false;
                break;
            }
        }
        if (!hasPermission) {
            LogUtils.e("进行了权限请求");
            requestPermissionsSafely(permissions, requestPermissionCode, activity);
        }
        return hasPermission;
    }

    /**
     * 判断是否需要申请权限
     * 两种情况下不需要申请
     * 1. 当前版本号小于23
     * 2. 申请的权限已经被授予
     * @param permission   需要申请的权限
     * @param activity
     *
     * PackageManager.PERMISSION_DENIED：拒绝了。
       PackageManager.PERMISSION_GRANTED：授权了

     * @return   返回true 表明已经授权，返回false 没有授权
     */
    @TargetApi(Build.VERSION_CODES.M)
    private boolean hasPermission(String permission, Activity activity) {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 申请相关权限
     */
    @TargetApi(Build.VERSION_CODES.M)
    private void requestPermissionsSafely(String[] permissions, int requestCode, Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(permissions, requestCode);
        }
    }

    /**
     * 权限申请结果的相应方法
     * @param requestCode
     * @param permissions
     * @param grantResults
     * @param activity
     * @return
     */
    public static boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, Activity activity) {
        boolean haspermission = true;
        if (requestCode == requestPermissionCode) {
            if(grantResults.length>0){
                for(int i= 0 ;i<grantResults.length;i++){
                    if(grantResults[i] == PackageManager.PERMISSION_DENIED ){
                        haspermission = false;
                        break;
                    }
                }
            }
        }
        return haspermission;
    }


//    private final Context mcontext;     // 长见识了
//
//    public PermissionsUtils(Context mcontext) {
//        this.mcontext = mcontext.getApplicationContext();
//    }
//
//    /**
//     *  判断权限集合
//     */
//    public boolean  lacksPermission(String... permissions){
//        for (String permission : permissions) {
//            if (lacksPermission(permission)) {
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     *  判断是否缺少权限
     */
//    private boolean lacksPermission(String permissions) {
//        return ContextCompat.checkSelfPermission(mcontext,permissions) == PackageManager.PERMISSION_DENIED;
//    }
}
